﻿using Microsoft.Extensions.Configuration.Json;
using S7.Net;
using System.IO;
using System.Text;

namespace DongFangGuoKai.WCSPlatform.WebApi.Utils
{
    /// <summary>
    /// 对数据的一些处理
    /// </summary>
    public static class DataTool
    {
        public static ushort seed = (ushort)new Random().Next(ushort.MaxValue); // 使用随机值初始化种子

        public static ushort GetTaskCode()
        {
            seed = (ushort)((seed * 1664525 + 1013904223) % ushort.MaxValue);
            return seed;
        }


        /// <summary>
        /// 根据窗口号找出对应的窗口号地址
        /// </summary>
        /// <param name="winCode">容器号</param>
        /// <returns></returns>
        public static string GetWinCodeDbAddress(ushort winCode,PlcDBAddress plcDBAddress)
        {
            switch (winCode)
            {
                case 95:
                    return plcDBAddress.Win1;
                case 94:
                    return plcDBAddress.Win2;
                case 93:
                    return plcDBAddress.Win3;
                case 92:
                    return plcDBAddress.Win4;
                case 91:
                    return plcDBAddress.Win5;
                case 90:
                    return plcDBAddress.Win6;
                default:
                    return "";
            }
        }
        /// <summary>
        /// 将字符串转换为字节数组 为string写扩展方法
        /// </summary>
        /// <param name="strVal"></param>
        /// <returns></returns>
        public static byte[] GetBytes(this string strVal)
        {
            return Encoding.ASCII.GetBytes(strVal);
        }
        /// <summary>
        /// 将字符转换为字节字符串
        /// </summary>
        /// <returns></returns>
        public static string ToHexStr(this string strVal)
        {
            byte[] val = GetBytes(strVal);
            return BitConverter.ToString(val).Replace("-", "");
        }
        /// <summary>
        /// 十六进制字符串转换回原始的字符串
        /// </summary>
        /// <param name="hexString">16进制字符串</param>
        /// <returns></returns>
        public static string HexToStr(this string hexString)
        {
            //在Plc中读出来的是"DB00300043313039393800000104CCEB" 我们只要其中的 "433130393938"
            string subString = hexString.Substring(8, 12); // 截取指定位置的子字符串
            byte[] bytes = new byte[subString.Length / 2];
            for (int i = 0; i < bytes.Length; i++)
            {
                bytes[i] = Convert.ToByte(subString.Substring(i * 2, 2), 16);
            }
            return Encoding.ASCII.GetString(bytes);
        }

        /// <summary>
        /// 十六进制字符串转换回原始的字符串
        /// </summary>
        /// <param name="hexString">16进制字符串</param>
        /// <returns></returns>
        public static string GetString(this byte[] hexString)
        {
            return Encoding.ASCII.GetString(hexString);
        }
        /// <summary>
        /// 读取字符串
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="db">DB块</param>
        /// <param name="addr">偏移量</param>
        /// <returns></returns>
        public static string ReadString(this Plc plc, int db, double addr)
        {
            string addr1 = "DB" + db.ToString() + ".DBB" + (addr + 1).ToString();
            int len = int.Parse(plc.Read(addr1).ToString());
            string getstring = "";
            for (int i = 0; i < len; i++)
            {
                string addr2 = "DB" + db.ToString() + ".DBB" + (addr + 2 + i).ToString();
                string result = plc.Read(addr2).ToString();
                int value = Convert.ToInt32(result, 10);
                string stringValue = Char.ConvertFromUtf32(value);
                getstring += stringValue;
            }
            return getstring;
        }
        /// <summary>
        /// 写入字符串
        /// </summary>
        /// <param name="plc"></param>
        /// <param name="db">DB块</param>
        /// <param name="addr">偏移量</param>
        /// <param name="data">数据</param>
        public static void WriteString(this Plc plc,int db, double addr, string data)
        {
            byte len = Convert.ToByte(data.Length);
            string addr1 = "DB" + db.ToString() + ".DBB" + (addr + 1).ToString();
            plc.Write(addr1, len);
            char[] single = data.ToCharArray();
            for (int i = 0; i < data.Length; i++)
            {
                string addr2 = "DB" + db.ToString() + ".DBB" + (addr + 2 + i).ToString();
                plc.Write(addr2, single[i].ToString());
            }
        }
    }

}
